package cn.impl;

import cn.annotation.Log;
import cn.basic.HibBaseDAOImpl;
import cn.basic.iHibBaseDAO;
import cn.common.properties.LogOperType;
import cn.dao.UserDAO;
import cn.model.TUser;
import cn.model.VUserRole;
import org.springframework.stereotype.Component;
import java.util.List;

@Component("userdao")
public class UserDAOImpl implements UserDAO {
	//连接数据库操作层接口,实现业务与数据的交换
	private iHibBaseDAO bdao= null;

	public UserDAOImpl(){
		bdao = new HibBaseDAOImpl();
	}

	/**
	 * 添加一个用户
	 *
	 * @param user 用户对象
	 * @return 添加成功返回受影响的行数
	 */
	@Log(isSaveLog=true,operationType=LogOperType.ADD,operationName="添加系统用户信息")
	@Override
	public int addUser(TUser user) {
		return (Integer) bdao.insert(user);
	}


	@Log(isSaveLog=true,operationType=LogOperType.DELETE,operationName="删除系统用户信息")
	@Override
	public boolean deleteUser(String userid) {
		TUser user = (TUser) bdao.findById(TUser.class, userid);
		return bdao.delete(user);
	}

	@Log(isSaveLog=true,operationType=LogOperType.MODIFY,operationName="修改系统用户信息")
	@Override
	public boolean modifyUser(TUser user) {
		return bdao.update(user);
	}

	@Log(isSaveLog=true,operationType=LogOperType.QUERY,operationName="查询用户信息")
	@Override
	public List<VUserRole> getUserList(String wherecondition, int currentPage,int pageSize) {
		String hql = "from VUserRole";
		if (wherecondition != null && !wherecondition.equals("")){
			hql += wherecondition;
		}
		if (wherecondition == null && wherecondition.equals("")){
			hql += "order by userid desc";
		}
		List<VUserRole> list = bdao.selectByPage(hql, currentPage, pageSize);
		return list;
	}

	@Log(isSaveLog=true,operationType=LogOperType.QUERY,operationName="查询用户信息数量")
	@Override
	public int getUserAmount(String wherecondition) {
		String hql = "select count(*) from VUserRole";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

	@Log(isSaveLog=true,operationType=LogOperType.MODIFY,operationName="修改用户密码")
	@Override
	public boolean Upedtpwd(String userid, String pwd) {
		String sql = "update T_User set pwd = ? where userid = ?";
		Object[] para = {pwd,userid};
		return bdao.update(sql, para);
	}

	@Log(isSaveLog=true,operationType=LogOperType.QUERY,operationName="通过用户编号查询用户")
	@Override
	public TUser getUserById(String userid) {
		return (TUser) bdao.findById(TUser.class, userid);
	}

	@Log(isSaveLog=true,operationType=LogOperType.QUERY,operationName="添加时先查询用户")
	@Override
	public int queryUserById(String userid){
		String hql = "select count(*) from TUser where userid = ?";
		Object[] para = {userid};
		return bdao.selectValue(hql,para);
	}

	@Log(isSaveLog=true,operationType=LogOperType.QUERY,operationName="通过用户编号查询用户")
	@Override
	public VUserRole getVUserRoleById(String userid) {
		return (VUserRole) bdao.findById(VUserRole.class, userid);
	}

	@Log(isSaveLog=true,operationType=LogOperType.QUERY,operationName="用户登录")
	@Override
	public List<VUserRole> userLogin(String userid, String pwd){
		String hql ="from VUserRole where userid=? and pwd =?";
		Object[] para = {userid,pwd};
		return bdao.select(hql, para);
	}

	@Log(isSaveLog=true,operationType=LogOperType.MODIFY,operationName="通过登录与退出来修改最后登录时间和登录状态")
	@Override
	public  boolean updateInfoByLogin(String lastlogindate,String loginstatus,String userid){
		String sql = "update T_User set lastlogindate = ? , loginstatus = ? where userid = ?";
		Object[] para = {lastlogindate,loginstatus,userid};
		return bdao.update(sql, para);
	}


}
